package utils
{
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.events.EventDispatcher;
	import flash.events.NativeProcessExitEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.utils.IDataInput;
	import flash.utils.getTimer;
	
	import events.AnimationEvent;
	
	import manager.LoggerManager;
	
	[Event(name="compiledFinished", type="events.AnimationEvent")]
	public class SWFCompilerUtil extends EventDispatcher{
		public static const CMDTYPE_DIR:String = "--dir";
		public static const CMDTYPE_FILE:String = "--file";
		public static const CMDTYPE_EACH_DIR:String = "--each.dir";
		public static const CMDTYPE_EACH_FILE:String = "--each.file";
		public static const CMDTYPE_FILES:String = "--files";
		
		private static var _self:SWFCompilerUtil;
		
		protected var isError:Boolean = false;
		
		public static function get instance():SWFCompilerUtil{
			if(_self==null){
				_self=new SWFCompilerUtil(new Single());
			}
			return _self;
		}
		
		private var _nativeProcessStartupInfo:NativeProcessStartupInfo;
		private var _process:NativeProcess;
		
		public function SWFCompilerUtil(s:Single){
		}
		
		public function delayStart(cmdType:String, outputDirUrl:String,fileUrl:String,urls:Array=null):void{
			isError = false;
//			setTimeout(function():void{
//				start(cmdType, outputDirUrl,fileUrl,urls);
//			},3000);
			start(cmdType, outputDirUrl,fileUrl,urls);
			LoggerManager.log("正在进行swf压缩，请耐心等待.....");
		}
		
		public function start(cmdType:String, outputDirUrl:String,fileUrl:String,urls:Array=null):void{
			FileUtil.createDir(outputDirUrl);
			_process = new NativeProcess();
			_process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onStandardOutputData);
			_process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onStandardErrorData);
			_process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
			var _file:File = File.applicationDirectory.resolvePath("SWFEmbedCompiler.exe");
			_nativeProcessStartupInfo = new NativeProcessStartupInfo();
			_nativeProcessStartupInfo.executable = _file;
			_nativeProcessStartupInfo.arguments[0] = cmdType;
			_nativeProcessStartupInfo.arguments[1] = outputDirUrl;
			_nativeProcessStartupInfo.arguments[2] = fileUrl;
			_nativeProcessStartupInfo.arguments[3] = fileUrl;
			if(urls != null && urls.length>0){
				for(var i:int=0;i<urls.length;i++){
					_nativeProcessStartupInfo.arguments[4+i] = urls[i];
				}
			}
			_process.start(_nativeProcessStartupInfo);
		}

		private function onExit(event:NativeProcessExitEvent):void{
			if(isError)
				return;
			LoggerManager.log("swf压缩完毕");
			dispatchEvent(new AnimationEvent(AnimationEvent.COMPILED_FINISHED));
		}
		
		private function onStandardErrorData(event:ProgressEvent):void{
			var data:IDataInput = _process.standardError;
			if(data.bytesAvailable == 0){
				return;
			}
			isError = true;
			LoggerManager.error("请配置正确的SDK路径");
			var str:String = data.readMultiByte(data.bytesAvailable, "chinese")
			trace("=========================>>>||||||||||||||||"+getTimer());
			trace(event.type, str);
		}
		
		private function onStandardOutputData(event:ProgressEvent):void{
			var data:IDataInput = _process.standardOutput;
			if(data.bytesAvailable == 0){
				return;
			}
			var str:String = data.readMultiByte(data.bytesAvailable, "chinese")
			trace("=========================>>>"+getTimer());
			trace(event.type, str);
		}
	}
}

class Single{}